#define _CRT_SECURE_NO_WARNINGS 1

#include<iostream>
#include<vector>

using namespace std;

int findMinDistance(vector<string>& arr, string str1, string str2) {
    int n = arr.size();
    int last1 = -1;       
    int last2 = -1;        
    int res = INT_MAX;

    for (int i = 0; i < n; i++) {
        if (arr[i] == str1) {
            if (last2 != -1) {  
                res = min(res, i - last2);
            }
            last1 = i;  
        }
        else if (arr[i] == str2) {  
            if (last1 != -1) { 
                res = min(res, i - last1);
            }
            last2 = i;  
        }
    }

    return (res == INT_MAX) ? -1 : res;
}